package com.seal.commons.generator;

import com.seal.commons.generator.config.GolbalConfig;
import com.seal.commons.generator.db.TemplateSql;
import com.seal.commons.generator.db.TemplateSqlFactory;
import com.seal.commons.generator.engine.TemplateEngine;
import com.seal.commons.generator.util.ConvertUtils;
import com.seal.commons.generator.vo.TableInfo;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-08-03 20:42<br/>
 * @since JDK 1.8
 */
public class CodeGenerator {

    private GolbalConfig golbalConfig;

    private TableInfo tableInfo;

    private TemplateEngine engine;

    private Map<String, Object> params;

    public CodeGenerator(GolbalConfig golbalConfig) throws SQLException {
        this.golbalConfig = golbalConfig;
        init();
    }


    public void generateCode() throws Exception {
        generateEntity();
        generateBO();
        generateRepository();
        generateService();
        generateServiceImpl();
        generateDTO();
    }

    public void generateEntity(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getEntityTemplatePath(), this.golbalConfig.getOutputDir() + "/entity/"+this.tableInfo.getEntityName() +".java");
    }

    public void generateBO(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getBoTemplatePath(), this.golbalConfig.getOutputDir() + "/bo/"+this.tableInfo.getBoName()+".java");
    }

    public void generateDTO(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getDtoTemplatePath(), this.golbalConfig.getOutputDir() + "/dto/"+this.tableInfo.getDtoName()+".java");
    }

    public void generateRepository(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getRepositoryTemplatePath(), this.golbalConfig.getOutputDir() + "/repository/"+this.tableInfo.getRepositoryName()+".java");
    }

    public void generateService(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getServiceTemplatePath(), this.golbalConfig.getOutputDir() + "/service/"+this.tableInfo.getServiceName()+".java");
    }

    public void generateServiceImpl(){
        engine.engine(params, this.golbalConfig.getTemplateConfig().getServiceImplTemplatePath(), this.golbalConfig.getOutputDir() + "/service/impl/"+this.tableInfo.getServiceImplName()+".java");
    }

    public void setGolbalConfig(GolbalConfig golbalConfig) throws SQLException {
        this.golbalConfig = golbalConfig;
        init();
    }



    private void init() throws SQLException {
        TemplateSql templateSql = TemplateSqlFactory.create(this.golbalConfig.getDatabaseConfig().getDbType());
        this.tableInfo = templateSql.getTable(this.golbalConfig.getTableName(), this.golbalConfig.getDatabaseConfig());
        new ConvertUtils(this.tableInfo, this.golbalConfig.getStrategyConfig()).convert();
        this.engine =  golbalConfig.getTemplateConfig().getEngine();
        this.params = new HashMap<>();
        params.put("tableInfo", tableInfo);
        params.put("auther", this.golbalConfig.getAuthor());
        params.put("package", this.golbalConfig.getPackageConfig());
        params.put("createDateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        params.put("strategyConfig", this.golbalConfig.getStrategyConfig() );
    }
}